package com.wlk.service.impl;

import com.wlk.mapper.StudentsMapper;
import com.wlk.pojo.Students;
import com.wlk.service.StudentsService;
import com.wlk.utils.Result;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.Year;
import java.util.ArrayList;
import java.util.List;


@Service
public class StudentsServiceImpl implements StudentsService {

            @Resource
            private StudentsMapper studentsMapper;

            /**
             * @description: 获取学生列表
             * @author: 王乐凯
             * @date: 2024/6/5 下午4:26
             * @param:
             * @return:
             **/
            public Result getStudentsList() {

            List<Students> studentList = studentsMapper.getAllStudents();

            if (studentList == null || studentList.size() == 0) {
                return Result.noData("学生列表为空");
            } else {
                return Result.ok(studentList);
            }
        }

            /**
             * @description: 分页查询学生列表
             * @author:
             * @date: 2024/6/9 上午1:49
             * @param:
             * @return:
             **/
            @Override
            public Result getStudentsByPage(Integer page, Integer size) {
            Integer offset = (page - 1) * size;
            List<Students> studentList = studentsMapper.getStudentsByPage(offset, size);
            if (studentList == null || studentList.size() == 0) {
                return Result.noData("学生列表为空");
            } else {
                return Result.ok(studentList);
            }
        }

            @Override
            @Transactional
            public Result importStudents(MultipartFile file) {
                try {
                    Workbook workbook = WorkbookFactory.create(file.getInputStream());
                    Sheet sheet = workbook.getSheetAt(0); // 假设数据在第一个sheet中
                    List<Students> students = new ArrayList<>();
                    for (Row row : sheet) {
                        if (row.getRowNum() == 0) continue; // 跳过表头
                        Students student = new Students();
                        student.setStudentName(row.getCell(0).getStringCellValue());
                        Cell cell = row.getCell(1); // 获取单元格
                        int gender;
                        if (cell.getCellType() == CellType.NUMERIC) {
                            // 如果是 NUMERIC 类型的单元格，使用 getNumericCellValue() 方法获取数值
                            gender = (int) cell.getNumericCellValue();
                        } else {
                            // 否则，将其视为字符串类型，并尝试将其解析为整数
                            gender = Integer.parseInt(cell.getStringCellValue());
                        }
                        student.setGender(gender);
                        student.setPhone(row.getCell(2).getStringCellValue());
                        student.setStudentNumber(String.valueOf(row.getCell(3).getStringCellValue()));
                        student.setEnrollmentYear(Year.parse(row.getCell(4).getStringCellValue()));
                        System.out.println("还是在方法中："+student);
                        students.add(student);
                    }

                    for (Students student : students) {
                        System.out.println("添加数据");
                        int i = studentsMapper.addStudents(student);
                        if (i <= 0) {
                            throw new Exception("导入失败");
                        }
                    }
                    return Result.ok("导入成功");
                } catch (Exception e) {
                    e.printStackTrace();
                    return Result.error("导入失败");
                }


            }


}
